/**
 * 判断括号是否有效
 * @param s
 */

function isValid(s) {
    let stack = []
    for (let i = 0; i < s.length; i++) {
        if (stack.length === 0) {
            stack.push(s[i])
        } else {
            if (isMatchV3(stack[stack.length - 1], s[i])) {
                stack.pop()
            } else {
                stack.push(s[i])
            }
        }
    }
    return stack.length === 0
}


function isMatch(chr1, chr2) {
    return chr1 === '(' && chr2 === ')' || chr1 === '{' && chr2 === '}' || chr1 === '[' && chr2 === ']';
}

function isMatchV3(L, R){
    // 左括号
    const left = ['(', '[', '{']
    // 右括号
    const right = [')', ']', '}']

    // 返回，左括号和右括号是否相对应
    return left.indexOf(L) === right.indexOf(R);
}

console.log(isValid('[[[]]]'));
console.log(isValid('[[{{}}]]}'));


function isValidV2(strs) {
    strs = strs.split('')
    const stack = []

    for (let item of strs) {
        if (stack.length === 0) {
            stack.push(item)
        } else {
            if (isMatchV2(stack[stack.length - 1], item)) {
                stack.pop()
            } else {
                stack.push(item)
            }
        }
    }
    return stack.length === 0
}

function isMatchV2(L, R) {
    return L === '(' && R === ')' || L === '[' && R === ']' || L === '{' && R === '}';

}

//
// console.log(isValidV2('[[[]]]'));
// console.log(isValidV2('[[{{}}]]}'));


function isVlidBrack(str){
    let stack = []
    for(let i=0;i<str.length; i++){
        if(stack.length ===0){
            stack.push(str[i])
        }else{
            if(isMatchV4(str[i], stack[stack.length-1])){
                stack.pop()
            }else{
                stack.push(str[i])
            }
        }
    }
    return stack.length ===0
}

function isMatchV4(S1, S2){
    let left = ['(','[','{'], right = [')', ']', '}']
    if (left.indexOf(S1) === right.indexOf(S2)){
        return true
    }
    return  false;
}

console.log('blank',isVlidBrack( '{{{{}}}}'));
console.log('blank',isVlidBrack( '{{{{[]}}}}'));
console.log('blank',isVlidBrack( '{{{{[}}}}'));


console.log(Object.create(null))
